// pages/u_Rorder/u_Rorder.js
const app = getApp()
var get_offer_order;var page = 1;var save_order_sd;var save_offer_price;

// 请求数据
var loadMore = function (that) {
  that.setData({
    hidden: false,
    stopdowning: true
  });
  wx.request({
    url: get_offer_order,
    data: {
      page: page,
      uid: that.data.aid,
      type:that.data.type
    },
    success: function (res) {
      console.log(res.data.list);
      var list = res.data.list;
      var goods = that.data.goods;
      if (list.length != 0) {
        for (var i = 0; i < list.length; i++) {
          goods.push(list[i]);
        }
      }
      that.setData({
        goods: goods,
        stopdowning: false
      });
      page++;
      if (goods.length == 0) {
        that.setData({
          hidden: true,
          stopdowning: true
        });
      } else {
        that.setData({
          hidden: false
        });
      }
    }
  });
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    head:[
      {name:'全部',type:0},
      {name:'已中标',type:1},
      {name:'待取件',type:2},
      {name:'运输中',type:3},
      {name:'待确认',type:4},
      {name:'已送达',type:5}
    ],
    head_index:0,
    goods: [],
    stopdowning: false,
    hidden: false,
    aid:'',
    type:'',
    price:'',
    did:'',
    fix:false,
  },
  hidetap:function(e){
    this.setData({
      fix:false
    })
  },
  price:function(e){
    this.setData({
      price:e.detail.value
    })
  },
  pricetap:function(e){
    var that = this;
    var price = e.currentTarget.dataset.price;
    var id = e.currentTarget.dataset.id;
    that.setData({
      price:price,
      did:id,
      fix:true
    })
  },
  submitBtn:function(e){
    var that = this;
    var aid = wx.getStorageSync('uid');
    var price = that.data.price;
    var did = that.data.did;
    if(Number(price) <= 0){
      wx.showModal({
        title: '提示',
        content: '报价金额须大于0',
        showCancel: false
      })
    }else{
      wx.showLoading();
      wx.request({
        url: save_offer_price,
        data: {
          uid:aid,
          price:price,
          id:did
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success (res) {
          console.log(res.data);
          wx.hideLoading();
          if(res.data.state == 1){
            page = 1;
            that.setData({
              goods: [],
              fix:false,
              price:'',
              stopdowning: false
            })
            if (!that.data.stopdowning) {
              loadMore(that);
            }
            wx.showModal({
              title: '提示',
              content: '修改成功！',
              showCancel: false
            })
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var type = options.type;
    var aid = wx.getStorageSync('uid');
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.windowHeight)
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    }) 
    get_offer_order = app.data.ucenter_url + 'get_offer_order';
    save_order_sd = app.data.ucenter_url + 'save_order_sd';
    save_offer_price = app.data.ucenter_url + 'save_offer_price';
    that.setData({
      head_index:type
    })
    var goods = [];page = 1;
    that.setData({
      goods: goods,
      stopdowning: false,
      aid:aid,
      type:type,
    })
    if (!that.data.stopdowning) {
      loadMore(that);
    }
  },
  headtap:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var type = e.currentTarget.dataset.type;
    var goods = [];page = 1;
    that.setData({
      goods: goods,
      stopdowning: false,
      type:type,
      head_index:index
    })
    if (!that.data.stopdowning) {
      loadMore(that);
    }
  },
  xqtap:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/i_detail/i_detail?id='+id,
    })
    // wx.navigateTo({
    //   url: '/pages/u_orderxq/u_orderxq?id='+id
    // })
  },
  songtap:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.request({
      url: save_order_sd,
      data: {
        id:id
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log(res.data);
        if(res.data.state == 1){
          wx.showModal({
            title: '提示',
            content: '操作成功！',
            showCancel: false,
            success:function(e){
              var goods = [];page = 1;
              that.setData({
                goods: goods,
                stopdowning: false
              })
              if (!that.data.stopdowning) {
                loadMore(that);
              }
            }
          })
        }
      }
    })
  },
  bindDownLoad: function () {
    var that = this;
    console.log(that.data.stopdowning);
    if (!that.data.stopdowning) {
      loadMore(that);
    } else {
      console.log('gg');
      return;
    }
  },
  scroll: function (event) {
    //该方法绑定了页面滚动时的事件，我这里记录了当前的position.y的值,为了请求数据之后把页面定位到这里来。
    this.setData({
      scrollTop: event.detail.scrollTop
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})